package com.ruoyi.system.service.impl;

import java.util.*;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.PmsBatchProduct;
import com.ruoyi.system.domain.PmsProduct;
import com.ruoyi.system.domain.vo.BatchEntityVo;
import com.ruoyi.system.domain.vo.BatchProductVo;
import com.ruoyi.system.mapper.PmsBatchProductMapper;
import com.ruoyi.system.mapper.PmsProductMapper;
import io.jsonwebtoken.lang.Collections;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.PmsBatchMapper;
import com.ruoyi.system.domain.PmsBatch;
import com.ruoyi.system.service.IPmsBatchService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

/**
 * 商品批次Service业务层处理
 *
 * @author ruoyi
 * @date 2024-11-11
 */
@Service
public class PmsBatchServiceImpl implements IPmsBatchService
{
    @Autowired
    private PmsBatchMapper pmsBatchMapper;
    @Autowired
    private PmsBatchProductMapper pmsBatchProductMapper;

    @Autowired
    private PmsProductMapper pmsProductMapper;

    /**
     * 查询商品批次
     *
     * @param batchId 商品批次主键
     * @return 商品批次
     */
    @Override
    public PmsBatch selectPmsBatchByBatchId(Long batchId)
    {
        return pmsBatchMapper.selectPmsBatchByBatchId(batchId);
    }

    /**
     * 查询商品批次列表
     *
     * @param pmsBatch 商品批次
     * @return 商品批次
     */
    @Override
    public List<PmsBatch> selectPmsBatchList(PmsBatch pmsBatch)
    {
        return pmsBatchMapper.selectPmsBatchList(pmsBatch);
    }

    /**
     * 新增商品批次
     *
     * @param pmsBatch 商品批次
     * @return 结果
     */
    @Override
    public int insertPmsBatch(PmsBatch pmsBatch)
    {
        pmsBatch.setCreateTime(DateUtils.getNowDate());
        return pmsBatchMapper.insertPmsBatch(pmsBatch);
    }

    /**
     * 修改商品批次
     *
     * @param pmsBatch 商品批次
     * @return 结果
     */
    @Override
    public int updatePmsBatch(PmsBatch pmsBatch)
    {
        pmsBatch.setUpdateTime(DateUtils.getNowDate());
        return pmsBatchMapper.updatePmsBatch(pmsBatch);
    }

    /**
     * 批量删除商品批次
     *
     * @param batchIds 需要删除的商品批次主键
     * @return 结果
     */
    @Override
    public int deletePmsBatchByBatchIds(Long[] batchIds)
    {
        return pmsBatchMapper.deletePmsBatchByBatchIds(batchIds);
    }

    /**
     * 删除商品批次信息
     *
     * @param batchId 商品批次主键
     * @return 结果
     */
    @Override
    public int deletePmsBatchByBatchId(Long batchId)
    {
        return pmsBatchMapper.deletePmsBatchByBatchId(batchId);
    }


//    @Override
//    public int addBatchProduct(BatchProductVo batchProductVo) {
//        PmsBatch pmsBatch = batchProductVo.getPmsBatch();
//        int j = pmsBatchMapper.insertPmsBatch(pmsBatch);
//        if(j>0){
//            if(!batchProductVo.getBatchProductList().isEmpty() && batchProductVo.getBatchProductList().size()>0){
//                List<PmsBatchProduct> pmsBatchProducts = batchProductVo.getBatchProductList();
//                for (int i=0;i<pmsBatchProducts.size();i++){
//                    pmsBatchProducts.get(i).setBatchId(pmsBatch.getBatchId());
//                    pmsBatchProductMapper.insertPmsBatchProduct(pmsBatchProducts.get(i));
//                }
//            }
//        }
//        return j;
//    }

    /**
     * 新增批次商品
     * @param batchProductVo
     * @return
     */
    @Override
    public int addBatchProduct(BatchProductVo batchProductVo) {
        PmsBatch pmsBatch = batchProductVo.getPmsBatch();
        int j = pmsBatchMapper.insertPmsBatch(pmsBatch);
        if(j>0){
            if(null != batchProductVo.getPmsProducts() && batchProductVo.getPmsProducts().size()>0){
                List<PmsProduct> products = batchProductVo.getPmsProducts();
                for (int i=0;i<products.size();i++){
                    products.get(i).setBatchId(pmsBatch.getBatchId());
                    pmsProductMapper.insertPmsProduct(products.get(i));
                }
            }
        }
        return j;
    }

    /**
     * 新增批次商品
     * @param batchProductVo
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateBatchProduct(BatchProductVo batchProductVo) {
        int j = pmsBatchMapper.updatePmsBatch(batchProductVo.getPmsBatch());
        PmsProduct p = new PmsProduct();
        p.setBatchId(batchProductVo.getPmsBatch().getBatchId());
        List<PmsProduct> getList = pmsProductMapper.selectPmsProductList(p);
        Iterator<PmsProduct> it = getList.iterator();

        List<PmsProduct> updateList = batchProductVo.getPmsProducts();
        List<PmsProduct> addList = new ArrayList<>();
        List<PmsProduct> editList = new ArrayList<>();
        List<PmsProduct> delList = new ArrayList<>();

        for(int i = 0;i<updateList.size();i++){
            if(null == updateList.get(i).getProductId()){
                addList.add(updateList.get(i));
            }
        }

        while (it.hasNext()){
            PmsProduct pmsProduct = it.next();
           if(!updateList.stream().anyMatch(obj->null!=obj.getProductId() ? obj.getProductId().equals(pmsProduct.getProductId()):true)){
               delList.add(pmsProduct);
           }
        }

        if(addList.size()>0){
//            pmsProductMapper.insertPmsProductList(addList);
            pmsProductMapper.insertPmsProductList(batchProductVo.getPmsBatch().getBatchId(),addList);
        }
//        getList.removeAll(editList);
        updateList.removeAll(addList);
        updateList.removeAll(delList);
        editList = updateList;
        if(delList.size()>0){
            pmsProductMapper.delPmsProductList(delList);
        }
        if(editList.size()>0){
            pmsProductMapper.updatePmsProductList(editList);
        }
        return j;
    }

    @Override
    public List<BatchProductVo> getBatchListVoAll(PmsBatch pmsBatch) {
        return pmsBatchMapper.getBatchListVoAll(pmsBatch);
    }


    @Override
    public List<BatchEntityVo> getBatchListAll(PmsBatch pmsBatch) {
        return pmsBatchMapper.getBatchListAll(pmsBatch);
    }

    @Override
    public BatchProductVo getBatchProductById(Long batchId) {
        return pmsBatchMapper.getBatchProductById(batchId);
    }
}
